package com.myfoon.action.backing.context;


import com.myfoon.action.backing.system.EventBean;
import com.myfoon.helper.entity.ThumbPhoto;
import com.myfoon.model.entity.Event;
import com.myfoon.model.entity.Location;
import com.myfoon.utils.CalendarUtils;
import com.myfoon.utils.ImageUtils;
import com.myfoon.view.application.MyConstants;
import com.myfoon.view.context.helper.BeanReceiver;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;

import javax.faces.event.ActionEvent;

import javax.faces.event.ActionListener;
import javax.faces.model.SelectItem;

import javax.imageio.ImageIO;

public class HomeBean {
    private String searchInput;
    private String sllValue;

//    private UploadedFile uploadedPhoto;
    private List<ThumbPhoto> uploadedPhotos;
    
    private int selectedDate;
    private int selectedMonth;
    private int selectedDay;
    private int selectedHour;
    private int selectedMinute;    
        
    private String eventMessage;
    private String divHtml;    
    
    private boolean mapvisible; 
    private boolean videovisible; 
    private boolean photovisible; 
    
    public final String staticImage = "/img/static/man.png";
                
    public HomeBean() {
        uploadedPhotos = new ArrayList<ThumbPhoto>();
        searchInput = "";    
        
        selectedDate = 2;
        selectedMonth = 0;
        selectedDay = 0;  
        selectedHour = 0;            
        selectedMinute = 0;     
        
        mapvisible = false;
    }


    public void setSearchInput(String searchInput) {
        this.searchInput = searchInput;
    }

    public String getSearchInput() {
        return searchInput;
    }    

    public void setSllValue(String sllValue) {
        this.sllValue = sllValue;
    }

    public String getSllValue() {
        return sllValue;
    }
/*
    public void setUploadedPhoto(UploadedFile uploadedPhoto) {
        this.uploadedPhoto = uploadedPhoto;
    }

    public UploadedFile getUploadedPhoto() {
        return uploadedPhoto;
    }

    public String doPhotoUpload() {
        try {
            if (uploadedPhoto != null) {
            }

            LoginBean loginBean =
                (LoginBean)BeanReceiver.receiveBean("LoginBean");
            String saveDirectory =
                MyConstants.photoUploadTempRealFolder + loginBean.getUser().getId().toString();
            String webFolder =
                MyConstants.photoUploadTempWebFolder + loginBean.getUser().getId().toString();

            String fileName = String.valueOf(new Date().getTime());
            fileName = fileName + ".png";
            saveToTempFolder(uploadedPhoto, saveDirectory, fileName);

            uploadedPhotos.add(new ThumbPhoto(saveDirectory + "/" + fileName,
                                              webFolder + "/" + fileName,
                                              fileName));
        } catch (Exception ex) {

        }

        return "ok";
    }
*/
/*
    public void saveToTempFolder(UploadedFile upFile, String saveDirectory,
                                 String fileName) {
        BufferedImage image;
        RenderedImage resized;
        int width = 75;
        int height;

        try {
            image = ImageIO.read(upFile.getInputStream());
            height = width * image.getHeight() / image.getWidth();

            resized = ImageUtils.resizeTrick(image, width, height);

            new File(saveDirectory + "/").mkdirs();

            File file = new File(saveDirectory + "/" + fileName);
            ImageIO.write(resized, "png", file);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setUploadedPhotos(List<ThumbPhoto> uploadedPhotos) {
        this.uploadedPhotos = uploadedPhotos;
    }

    public List<ThumbPhoto> getUploadedPhotos() {
        return uploadedPhotos;
    }
                
    public void setDivHtml(String divHtml) {
        this.divHtml = divHtml;
    }

    public String getDivHtml() {
        return divHtml;
    }
    
    public String findSearchResults() {   
        MapBean bean = new MapBean();
        List<Location> locList = bean.findLocations(searchInput, sllValue);
                
        String resultsHtml = bean.prepareDivHtml(locList);
        setDivHtml(resultsHtml);
        
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExtendedRenderKitService erks = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);

        erks.addScript(facesContext, "var srh = window.document.getElementById('searchResultsHtml'); srh.onchange();");
        
        System.out.println(searchInput);
        System.out.println(sllValue);
        return "success";
    }
    
    public void findSearchResultsListener(ActionEvent event)
    {
        MapBean bean = new MapBean();
        List<Location> locList = bean.findLocations(searchInput, sllValue);
                
        String resultsHtml = bean.prepareDivHtml(locList);
        setDivHtml(resultsHtml);
        
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExtendedRenderKitService erks = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);

        erks.addScript(facesContext, "var srh = window.document.getElementById('searchResultsHtml'); srh.onchange();");
        
        System.out.println(searchInput);
        System.out.println(sllValue);        
    }
    
    
    public List<SelectItem> getMonthList() {
        return CalendarUtils.getMonthList();
    }
    
    public List<SelectItem> getDayList() {
        return CalendarUtils.getDayList();
    }
    
    public List<SelectItem> getHourList() {
        return CalendarUtils.getHourList();
    }
    
    public List<SelectItem> getMinuteList() {
        return CalendarUtils.getMinuteList();
    }

    public void setSelectedMonth(int selectedMonth) {
        this.selectedMonth = selectedMonth;
    }

    public int getSelectedMonth() {
        return selectedMonth;
    }

    public void setSelectedDay(int selectedDay) {
        this.selectedDay = selectedDay;
    }

    public int getSelectedDay() {
        return selectedDay;
    }

    public void setSelectedHour(int selectedHour) {
        this.selectedHour = selectedHour;
    }

    public int getSelectedHour() {
        return selectedHour;
    }

    public void setSelectedMinute(int selectedMinute) {
        this.selectedMinute = selectedMinute;
    }

    public int getSelectedMinute() {
        return selectedMinute;
    }

    public void setSelectedDate(int selectedDate) {
        this.selectedDate = selectedDate;
    }

    public int getSelectedDate() {
        return selectedDate;
    }
    
    public void saveEvent(ActionEvent action) {
        EventBean eventBean = new EventBean();                    
        LoginBean loginBean = (LoginBean)BeanReceiver.receiveBean("LoginBean");                
        
        Event event = new Event();                
        event.setOwner(loginBean.getUser());        
        event.setMessage(eventMessage);
        
        eventBean.persist(event);        
    }

    public void setEventMessage(String eventMessage) {
        this.eventMessage = eventMessage;
    }

    public String getEventMessage() {
        return eventMessage;
    }
     
    public List<Event> getAllEvents() {
        EventBean eventBean = new EventBean();
        return eventBean.findAllByRange(0, 15);
        /*
        List<Event> eventList = new ArrayList<Event>();
        
        for (int i = 0;  i <= 15; i++) {
            Event e = new Event();            
            e.setMessage("Message Message Message " + i);
            eventList.add(e);
            e.getOwner().getPhotoNormal()
        }        
        return eventList;
    }
*/
    public String getStaticImage() {
        return staticImage;
    }

    public void setMapvisible(boolean mapvisible) {
        this.mapvisible = mapvisible;
    }

    public boolean isMapvisible() {
        return mapvisible;
    }
            
    public String showPhotoBox() {
        setPhotovisible(true);
        setMapvisible(false);          
        setVideovisible(false);
        return "ok";
    }
    
    public String showMapBox() {
        setPhotovisible(false);
        setMapvisible(true);          
        setVideovisible(false);
        return "ok";
    }
    
    public String showVideoBox() {
        setPhotovisible(false);
        setMapvisible(false);          
        setVideovisible(true);
        return "ok";
    }

    public void setVideovisible(boolean videovisible) {
        this.videovisible = videovisible;
    }

    public boolean isVideovisible() {
        return videovisible;
    }

    public void setPhotovisible(boolean photovisible) {
        this.photovisible = photovisible;
    }

    public boolean isPhotovisible() {
        return photovisible;
    }
}
